package org.smoozik.app.mode.linetrainer;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;

import org.smoozik.app.ihm.Constants;

public class LineTrainerListener implements KeyListener, ActionListener {
	LineTrainerMode mode;

	public LineTrainerListener(LineTrainerMode mode) {
		super();
		this.mode = mode;
	}

	public void keyPressed(KeyEvent e) {
	}

	public void keyTyped(KeyEvent e) {
	}

	public void keyReleased(KeyEvent e) {
		int c = e.getKeyCode();

		for (int i = 0; i < Constants.NOTE_LABELS.length; i++) {
			if (c == Constants.noteKeys[i]) {
				mode.getTypedNoteLabel().setText(Constants.NOTE_LABELS[i]);
				mode.validateAnswer();
				return;
			}
		}

		if (c == KeyEvent.VK_SPACE) {
			mode.start();
		} else {
			// not a note key
		}
	}

	public void actionPerformed(ActionEvent e) {
		mode.getPanel().requestFocusInWindow();

		Object source = e.getSource();
		if (source instanceof JButton) {
			JButton button = (JButton) source;
			String labelButton = button.getText();
			if (Constants.RESET_LABEL.equals(labelButton)) {
				mode.resteNotes();
			} else if (Constants.PLUS_LABEL.equals(labelButton)) {
				mode.increaseSpeed();
			} else if (Constants.MINUS_LABEL.equals(labelButton)) {
				mode.decreaseSpeed();
			} else if (Constants.STOP_LABEL.equals(labelButton)) {
				mode.endGame();
				// mode.stopTimer();
			} else if (Constants.START_LABEL.equals(labelButton)) {
				mode.start();
			} else if (Constants.REPLAY_LABEL.equals(labelButton)) {
				mode.playFigure();
			} else if (Constants.GO_LABEL.equals(labelButton)) {
				mode.validateAnswer();
			} else if (Constants.SHARP_LABEL.equals(labelButton)) {
				mode.sharp();
			} else if (Constants.BEMOL_LABEL.equals(labelButton)) {
				mode.bemol();
			} else if (Constants.MINOR_LABEL.equals(labelButton)) {
				mode.minor();
			} else if (Constants.MAJOR_LABEL.equals(labelButton)) {
				mode.major();
			} else {
				mode.getTypedNoteLabel().setText(labelButton);
				mode.noteTyped(labelButton);
			}
		}
	}

}
